<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script type="text/javascript" src="/js/plugins/form/jquery.form.js"></script>
    <script src="js/common.js"></script>

    <script>
        $(function () {
            //获取url的攻略id
            var param = getParams();
            if (param.id) {
                //如果存在id,先查询当前攻略信息
                $.get('/strategies/' + param.id, function (data) {
                    $('#strategy-h5-title').html(data.title);
                });

                //获取该攻略下的分类
                $.get('/strategies/' + param.id + '/strategyCatalog', function (data) {
                    var count = 1;
                    $('.catalogs').renderValues({list: data}, {
                        getName: function (item, value) {
                            $(item).html(count + '/' + value);
                            count++;
                        },
                        getHref: function (item, value) {
                            var href = $(item).data('href');
                            $(item).attr('href', href + value)
                        }
                    })
                });


                //查询某个攻略的常用标签
                $.get('/strategies/' + param.id + '/tags', function (data) {
                    $('.tag').renderValues(data)
                });


                //总页数
                var pages;
                //声明一个变量,用于当前页的增加
                var currentPage = 1;

                //存储评论的数组
                var commentArr = [];

                //当点击点评时再查询评论
                $('#pills-comment-tab').click(function () {
                    query()
                });

                //定义一个函数,函数里发请求,使得变量的值仅有限于该次访问
                function query() {
                    $.get('/strategies/' + param.id + '/strategyComments', {currentPage: currentPage}, function (data) {
                        $.merge(commentArr, data.list);

                        $('#comment').renderValues({list: commentArr}, {
                            getHref: function (item, value) {
                                $(item).attr('href', $(item).data('href') + value)
                            },
                            getStar: function (item, value) {
                                var starHtml = "";
                                for (i = 0; i < value; i++) {
                                    starHtml += '<i class="fa fa-star"></i>'
                                }
                                for (i = 0; i < 5 - value; i++) {
                                    starHtml += '<i class="fa fa-star-o"></i>'
                                }
                                $(item).html(starHtml)
                            }
                        });
                        //获取总页数
                        pages = data.pages;
                    });
                    //当前页加1
                    currentPage = currentPage + 1;
                }


                //滚动事件
                $(window).scroll(function () {
                    if ($(window).height() + $(document).scrollTop() + 1 >= $(document).height()) {
                        if (currentPage <= pages) {
                            query();
                        } else {
                            $(document).dialog({
                                type: 'notice',
                                content: '<span class="info-text">已经到底了</span>',
                                autoClose: 2000
                            })
                        }
                    }
                });


                //点评事件
                $('#commentBtn').click(function () {
                    window.location.href = '/mine/addComment.html?id=' + param.id;
                })

                //每次进入这个页面 都让访问量加1
                $.post("/collects/views",{'strategy.id':param.id},function (data) {

                })
                //获取当前登录用户的id
                var user = JSON.parse(sessionStorage.getItem("user"));
                console.log(user);
                //收藏数量显示
                if(user==null){
                    //如果用户没有登录 显示普通状态
                    $.get("/collects/",{'strategy.id':param.id}, function (data) {
                        console.log(data);
                        //设置浏览量
                        $("#view").html(data.views);
                        //设置点赞总数
                        $("#collect").html(data.collects);

                    })
                }else {
                    //如果用户登录 判断是否点赞
                    $.get("/collects/",{'strategy.id':param.id,'user.id':user.id}, function (data) {
                        //判断当前用户是否收藏
                        //有收藏
                        if(data.isCollect){
                            //设置背景颜色
                            $(".collect").attr("style","");
                            //设置点赞总数
                            $("#collect").html(data.collects);
                            //设置文字
                            $("#collectText").html("已收藏");
                            //设置浏览量
                            $("#view").html(data.views);
                        }
                        //设置浏览量
                        $("#view").html(data.views);
                        //设置点赞总数
                        $("#collect").html(data.collects);
                    })

                }






                //绑定点击收藏事件

                $(".collect").click(function () {
                    if (user!=null) {
                        //发送请求获取当前文章的点赞信息
                        $.ajax({
                            url: '/collects',
                            type: 'put',
                            data: {
                                'strategy.id': param.id,
                                'user.id': user.id
                            },
                            success: function (susu) {
                                $.get("/collects/",{'strategy.id':param.id,'user.id':user.id}, function (data) {
                                    //判断当前用户是否收藏
                                    //有收藏
                                    if(susu.success){
                                        //设置背景颜色
                                        $(".collect").attr("style","");
                                        //设置点赞总数
                                        $("#collect").html(data.collects);
                                        //设置文字
                                        $("#collectText").html("已收藏");
                                        //设置浏览量
                                        $("#view").html(data.views);
                                    }else {
                                        $(".collect").attr("style","color: #1d1c1c");
                                        $("#collectText").html("收藏");
                                        //设置浏览量
                                        $("#view").html(data.views);
                                        //设置点赞总数
                                        $("#collect").html(data.collects);
                                    }

                                })

                            },
                        })
                    } else {
                        $(document).dialog({
                            type: 'notice',
                            content: '<span class="info-text">亲,请先登录</span>',
                            autoClose: 2000
                        })
                    }
                })
                //提示用户登录


            }
        })
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略">
            </div>
        </div>
    </div>
</div>

<div class="container strategies">
    <h5 class="title" id="strategy-h5-title"></h5>
    <small><span id="view">0</span>人阅读过此攻略</small>
    <div class="focus">
        <i class="fa fa-heart collect" style="color: #1d1c1c"><span id="collect"> 0</span> <span
                id="collectText">收藏</span></i>
        <i class="fa fa-thumbs-up"><span> 0</span> 点赞</i>
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">
        <div class="container hot">
            <h6>大家都在看</h6>
            <div>
                <a href="travelContent.html">
                    <button class="btn">上下九</button>
                </a><a href="travelContent.html">
                <button class="btn">入住长隆熊猫酒店，天天都是儿童节</button>
            </a><a href="travelContent.html">
                <button class="btn">飞到羊城，只为一场最地道的早茶之旅</button>
            </a></div>
        </div>
        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-fun="getName" render-key="list.name"></h6>
                    <div render-loop="list.listDetails">
                        <a data-href="strategyDetails.html?id=" render-fun="getHref" render-key="list.listDetails.id">
                            <button class="btn" render-html="list.listDetails.title"></button>
                        </a>

                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div render-loop="list">
                <span render-html="list.name"></span>
            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a data-href="userProfiles.html?id=" render-fun="getHref" render-key="list.user.id">
                            <img class="rounded-circle" render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.user.nickName"></span></p>
                        <span class="comment-star" render-fun="getStar" render-key="list.starNum">
                            <i class="fa fa-star"></i>
                            <i class="fa fa-star"></i>
                            <i class="fa fa-star"></i>
                            <i class="fa fa-star"></i>
                            <i class="fa fa-star-o"></i>
                        </span>
                        <span class="comment-date" render-html="list.createTime"></span>
                        <div class="comment-content">
                            <!-- 查询具体的评论信息 -->
                            <a data-href="strategyComment.html?id=" render-fun="getHref" render-key="list.id">
                                <p render-html="list.content"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-loop="list.imgArr">
                            <li>
                                <img render-src="list.imgArr.self">
                            </li>
                        </ul>

                        <div class="d-flex icon">
                            <a href="strategyComment.html">
                                <i class="fa  fa-commenting-o " style="margin-right: 10px"> 评论 </i>
                                <i class="fa fa-thumbs-o-up"> <span>0</span> </i>
                            </a>
                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>

</div>
</div>

<div class="operation">
    <button class="btn">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>


</body>
</html>